//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities.Properties;
    using Microsoft.Practices.Unity;
    using SR = Microsoft.Research.DataLayer;
    using System.Collections.Generic;

    /// <summary>
    /// The presenter for the left pane Group tree.
    /// </summary>
    public class GroupLeftPanePresenter : RegistryManagerLeftPaneItemPresenterBase
    {
        /// <summary>
        /// Gets or sets the root model.
        /// </summary>
        /// <value>The root model.</value>
        public override BaseRegMgrModel RootModel
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowLeftPanePresenter"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="leftPane">The left pane.</param>
        /// <param name="service">The service.</param>
        public GroupLeftPanePresenter(IUnityContainer container, GroupTree leftPane, IRegistryManagerService service)
            : base(container, service)
        {
            this.LeftPaneView = leftPane;
            leftPane.Model = this;
        }

        #region IRegistryManagerLeftPaneItemPresenter Members

        /// <summary>
        /// Gets or sets the search view.
        /// </summary>
        /// <value>The search view.</value>
        public override UIElement SearchView
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the left pane view.
        /// </summary>
        /// <value>The left pane view.</value>
        public override UIElement LeftPaneView
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public override void Initialize()
        {
            try
            {
                if (this.LeftPaneTreeItems.Count > 0)
                {
                    this.LeftPaneTreeItems.Clear();
                }

                this.BuildTree();
            }
            catch (TridentCustomException ex)
            {
                TridentErrorHandler.HandleTridentCustomExceptionInUI(ex);
            }
        }

        #endregion

        /// <summary>
        /// Builds the Group Tree
        /// </summary>
        private void BuildTree()
        {
            try
            {
                Collection<SR.Group> groups = this.RegistryManagerService.GetAllGroups();
                GroupViewModel baseGroup = new GroupViewModel();
                baseGroup.Name = ManagementStudio.VirtualGroupName;
                baseGroup.Id = Guid.NewGuid();
                baseGroup.NonVirtualGroup = false;
                this.LeftPaneTreeItems.Add(baseGroup);
                foreach (SR.Group group in groups)
                {
                    GroupViewModel groupModel = new GroupViewModel(group, baseGroup);
                    baseGroup.Children.Add(groupModel);
                }

                this.RootModel = baseGroup;
            }
            catch (TridentCustomException ex)
            {
                TridentErrorHandler.HandleTridentCustomExceptionInUI(ex);
            }
        }

        /// <summary>
        /// Refreshes this instance.
        /// </summary>
        public override void Refresh(BaseRegMgrModel selectedModel)
        {
            this.Initialize();
            if (selectedModel != null)
            {
                selectedModel = selectedModel.GetModelById(selectedModel, this.LeftPaneTreeItems);
                if (selectedModel != null)
                {
                    base.ItemSelectedCommand.Execute(selectedModel);
                }
                else if (RootModel != null)
                {
                    base.ItemSelectedCommand.Execute(RootModel);
                }
            }
        }
    }
}
